package breeze.library.orchid.utils;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;

import org.json.JSONObject;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import breeze.library.orchid.web.OrchidWebUtils;

public class OrchidAppUpdate {

    /*
     *@author BREEZE
     *@date 2021-08-22 12:00:06
     */
    private static String appLink = "";
    private static Context appContext = null;
    private static OrchidAppUpdateBean data;
    private static OrchidAppUpdateListener mListener;
    private static final ExecutorService executorService = Executors.newFixedThreadPool(5);

    /**
     * @param context  上下文
     * @param link     更新请求连接
     * @param listener 更新监听器
     */
    public static void setOnUpdateListener(Context context, String link, OrchidAppUpdateListener listener) {
        appContext = context;
        appLink = link;
        mListener = listener;
        executorService.submit(() -> {
            try {
                String result = OrchidWebUtils.getWebData(appLink, null, null);
                if (result.isEmpty()) {
                    threadFinished(null);
                } else {
                    JSONObject obj = new JSONObject(result);
                    threadFinished(obj);
                }
            } catch (Exception e) {
                mListener.onError(e);
                threadFinished(null);
            }
        });
    }

    private static void threadFinished(final JSONObject result) {
        Handler handler = new Handler(Looper.getMainLooper());
        handler.post(() -> {
            if (result != null) {
                data = new OrchidAppUpdateBean();
                try {
                    data.setAppName(result.getString("appName"));
                    data.setAppVersion(result.getInt("appVersion"));
                    data.setAppVersionName(result.getString("appVersionName"));
                    data.setDownLink(result.getString("downLink"));
                    data.setUpdateContent(result.getString("updateContent"));
                    data.setFileSize(result.getLong("filesize"));
                    int appVersion = OrchidAppUtils.getVersionCode(appContext);
                    if (appVersion >= data.getAppVersion()) {
                        //没有更新
                        mListener.noNewVersion();
                    } else {
                        //发现更新
                        mListener.haveNewVersion(data);
                    }
                } catch (Exception e) {
                    mListener.onError(e);
                }
            }
        });
    }

    public static class OrchidAppUpdateBean {
        private String appName, appVersionName, updateContent, downLink;
        private int appVersion;
        private long fileSize;

        public void setAppName(String appName) {
            this.appName = appName;
        }

        public String getAppName() {
            return appName;
        }

        public void setAppVersionName(String appVersionName) {
            this.appVersionName = appVersionName;
        }

        public String getAppVersionName() {
            return appVersionName;
        }

        public void setUpdateContent(String updateContent) {
            this.updateContent = updateContent;
        }

        public String getUpdateContent() {
            return updateContent;
        }

        public void setDownLink(String downLink) {
            this.downLink = downLink;
        }

        public String getDownLink() {
            return downLink;
        }

        public void setAppVersion(int appVersion) {
            this.appVersion = appVersion;
        }

        public int getAppVersion() {
            return appVersion;
        }

        public long getFileSize() {
            return fileSize;
        }

        public void setFileSize(long fileSize) {
            this.fileSize = fileSize;
        }
    }

    public interface OrchidAppUpdateListener {
        void haveNewVersion(OrchidAppUpdateBean info);

        void noNewVersion();

        void onError(Exception exception);
    }

}
